package org.chengyi.model_shop.mapper;

import org.apache.ibatis.annotations.*;
import org.chengyi.model_shop.entity.Product;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface ProductMapper {
    @Insert("INSERT INTO product (name, description, price, stock,image_url, category_id, created_at) " +
            "VALUES (#{name}, #{description}, #{price}, #{stock}, #{imageUrl}, #{categoryId}, NOW())")
    int addProduct(Product product);
    @Select("select * from product where name like concat('%',#{name},'%')")
    List<Product> getByName(String name);
    @Delete("delete from product where id=#{id}")
    int delete(int id);
    @Select("select * from product where id=#{id}")
    Product getById(int id);
    @Update("update product set name=#{name}, description=#{description}, price=#{price}, stock=#{stock},image_url=#{imageUrl}, category_id=#{categoryId} where id=#{id}")
    int update(Product product);
    @Select("select description from product where id=#{id}")
    String getDescriptionById(int id);

    @Select("SELECT price FROM product WHERE id = #{id}")
    Double getPriceById(Integer id);

    // 按分类查询（分页由PageHelper自动处理）
    List<Product> getProductListByCategory(@Param("categoryId") Integer categoryId);

    @Update("UPDATE product SET stock = stock - #{quantity} WHERE id = #{productId} AND stock >= #{quantity}")
    int deleteProductStockById(@Param("productId") Integer productId, @Param("quantity") Integer quantity);

    @Update("UPDATE product SET stock = stock + #{quantity} WHERE id = #{productId}")
    int increaseStock(@Param("productId") Integer productId, @Param("quantity") Integer quantity);
}
